package com.lib.common.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.LinkedList;

/**
 * @author FengLing
 * Created on 2017-02-14 17:26
 */
public abstract class PagerBaseAdapter<T extends View> extends PagerAdapter {

    // 被回收的View集合
    private LinkedList<View> mRecycledViews;

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View view;
        if (mRecycledViews == null || (view = mRecycledViews.poll()) == null) {
            view = onCreateView(container, position);
        }
        onBindView((T) view, position);
        view.setTag(position);
        container.addView(view);
        return view;
    }

    public abstract View onCreateView(ViewGroup container, int position);

    public abstract void onBindView(T t, int position);

    public int getItemPosition(View view) {

        return ((int) view.getTag());
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {

        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        if (mRecycledViews == null) {
            mRecycledViews = new LinkedList<>();
        }
        View view = (View) object;
        mRecycledViews.add(view);
        container.removeView(view);
    }
}

/*






 package com.tx.txgxjk.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.tx.txgxjk.util.LogUtils;

import java.util.LinkedList;

/**
 * @author FengLing
 * Created on 2017-02-14 17:26
 *
public abstract class PagerBaseAdapter<T extends View> extends PagerAdapter {

	// 被回收的View集合
	private LinkedList<View> mRecycledViews;

	@NonNull
	@Override
	public Object instantiateItem(@NonNull ViewGroup container, int position) {

		View view;

		if (mRecycledViews == null
				|| (view = mRecycledViews.peek()) == null
				|| (!checkReuseViewIfValid(view, position))) {
			view = onCreateView(container, position);
			view.setTag(position);
		} else {
			view = mRecycledViews.poll();
			LogUtils.i("999 已复用->" + view.getClass().getSimpleName() + " " + view.getTag());
		}
		onBindData((T) view, position);
		view.setTag(position);
		container.addView(view);
		return view;
	}

	/**
	 * @param view     被复用的视图
	 * @param position 所在位置
	 * @return
	 *
	public abstract boolean checkReuseViewIfValid(View view, int position);

	/**
	 * 创建新的视图
	 *
	 * @param container
	 * @param position
	 * @return
	 *
	public abstract View onCreateView(ViewGroup container, int position);

	/**
	 * @param t        绑定数据
	 * @param position
	 *
	public abstract void onBindData(T t, int position);

	public int getItemPosition(View view) {

		return ((int) view.getTag());
	}

	@Override
	public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {

		return view == object;
	}

	@Override
	public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

		if (mRecycledViews == null) {
			mRecycledViews = new LinkedList<>();
		}
		View view = (View) object;

		mRecycledViews.add(view);
		if (mRecycledViews.size() > 20) {
			mRecycledViews.poll();
		}
		LogUtils.i("999 ", "位置：" + position + " 已回收：" + mRecycledViews.size() + " destroyItem_view->" + view);
		container.removeView(view);
	}
}

*/